<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - gui_core_kernel_1.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2005  Davis E. King (davis@dlib.net), Keita Mochizuki
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_GUI_CORE_KERNEL_1_CPp_
<font color='#0000FF'>#define</font> DLIB_GUI_CORE_KERNEL_1_CPp_
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../platform.h.html'>../platform.h</a>"

<font color='#0000FF'>#ifdef</font> WIN32

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='gui_core_kernel_1.h.html'>gui_core_kernel_1.h</a>"

<font color='#009900'>// tell visual studio to link to the libraries we need if we are
</font><font color='#009900'>// in fact using visual studio
</font><font color='#0000FF'>#ifdef</font> _MSC_VER
<font color='#0000FF'>#pragma</font> comment <font face='Lucida Console'>(</font>lib, "<font color='#CC0000'>gdi32.lib</font>"<font face='Lucida Console'>)</font>
<font color='#0000FF'>#pragma</font> comment <font face='Lucida Console'>(</font>lib, "<font color='#CC0000'>comctl32.lib</font>"<font face='Lucida Console'>)</font>
<font color='#0000FF'>#pragma</font> comment <font face='Lucida Console'>(</font>lib, "<font color='#CC0000'>user32.lib</font>"<font face='Lucida Console'>)</font>
<font color='#0000FF'>#pragma</font> comment <font face='Lucida Console'>(</font>lib, "<font color='#CC0000'>imm32.lib</font>"<font face='Lucida Console'>)</font>
<font color='#0000FF'>#endif</font>


<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>sstream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../threads.h.html'>../threads.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../assert.h.html'>../assert.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../queue.h.html'>../queue.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../sync_extension.h.html'>../sync_extension.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../queue.h.html'>../queue.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../logger.h.html'>../logger.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>cmath<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>vector<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals
    <b>{</b>


        <font color='#0000FF'>struct</font> <b><a name='user_event_type'></a>user_event_type</b>
        <b>{</b>
            HWND w;
            <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> p;
            <font color='#0000FF'><u>int</u></font> i;
        <b>}</b>;

        <font color='#0000FF'>typedef</font> sync_extension<font color='#5555FF'>&lt;</font>binary_search_tree<font color='#5555FF'>&lt;</font>HWND,base_window<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font>::kernel_1a<font color='#5555FF'>&gt;</font>::kernel_1a window_table_type;
        <font color='#0000FF'>typedef</font> sync_extension<font color='#5555FF'>&lt;</font>queue<font color='#5555FF'>&lt;</font>user_event_type,memory_manager<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font>::kernel_1b<font color='#5555FF'>&gt;</font>::kernel_2a_c<font color='#5555FF'>&gt;</font>::kernel_1a queue_of_user_events;


        <font color='#0000FF'>enum</font> <b><a name='USER_OFFSETS'></a>USER_OFFSETS</b>
        <b>{</b>
            CREATE_WINDOW,
            DESTROY_WINDOW,
            SET_ACTIVE_WINDOW,
            QUIT_EVENT_HANDLER_THREAD,
            USER_EVENTS_READY,
            CALL_MOVE_WINDOW,
            SHOW_WINDOW_SHOW,
            SHOW_WINDOW_HIDE,
            CALL_SET_WINDOW_TITLE
        <b>}</b>;

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>const</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>dlib::mutex<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> <b><a name='global_mutex'></a>global_mutex</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>static</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>dlib::mutex<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> dlib::mutex<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> m;
        <b>}</b>

        <font color='#0000FF'>class</font> <b><a name='event_handler_thread'></a>event_handler_thread</b> : <font color='#0000FF'>public</font> threaded_object
        <b>{</b>
        <font color='#0000FF'>public</font>:

            <font color='#0000FF'>enum</font> <b><a name='et_state'></a>et_state</b>
            <b>{</b>
                uninitialized,
                initialized,
                failure_to_init 
            <b>}</b>;
            
            et_state status;

            queue_of_user_events user_events;
            queue_of_user_events user_events_temp;
            logger dlog;

            HINSTANCE hInstance;
            HWND helper_window;    
            <font color='#0000FF'>const</font> TCHAR<font color='#5555FF'>*</font> window_class_name;

            <font color='#0000FF'><u>bool</u></font> quit_windows_loop;
            <font color='#0000FF'><u>bool</u></font> set_window_title_done;
            std::wstring window_title;
            <font color='#0000FF'><u>bool</u></font> move_window_done;
            HWND move_window_hwnd;
            <font color='#0000FF'><u>int</u></font> move_window_width;
            <font color='#0000FF'><u>int</u></font> move_window_height;
            <font color='#0000FF'><u>int</u></font> move_window_x;
            <font color='#0000FF'><u>int</u></font> move_window_y;
            <font color='#0000FF'><u>bool</u></font> request_new_window;
            DWORD dwStyle;
            HWND new_window;
            <font color='#0000FF'><u>bool</u></font> in_ime_composition;
            <font color='#0000FF'><u>bool</u></font> event_thread_started;
            <font color='#009900'>// the window_table.get_mutex() mutex locks the above 11 variables
</font>

            <font color='#009900'>// this variable holds a mapping from window handles to the base_window
</font>            <font color='#009900'>// objects which represent them.  Note that this objects mutex is always locked
</font>            <font color='#009900'>// when inside the event loop.
</font>            window_table_type window_table;
            rsignaler window_close_signaler;
            rsignaler et_signaler;

            <font color='#009900'>// note that this is the thread that will perform all the event
</font>            <font color='#009900'>// processing.
</font>            thread_id_type event_thread_id;

            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>dlib::mutex<font color='#5555FF'>&gt;</font> reference_to_global_mutex;

            <b><a name='event_handler_thread'></a>event_handler_thread</b><font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> :
                dlog<font face='Lucida Console'>(</font>"<font color='#CC0000'>dlib.gui_core</font>"<font face='Lucida Console'>)</font>,
                hInstance<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                helper_window<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                window_class_name<font face='Lucida Console'>(</font>TEXT <font face='Lucida Console'>(</font>"<font color='#CC0000'>w3049u6qc2d94thw9m34f4we0gvwa3-tgkser0-b9gm 05</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                quit_windows_loop<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
                set_window_title_done<font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>,
                move_window_done<font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>,
                move_window_hwnd<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                move_window_width<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                move_window_height<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                move_window_x<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                move_window_y<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                request_new_window<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
                dwStyle<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                new_window<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                in_ime_composition<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
                event_thread_started<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
                window_close_signaler<font face='Lucida Console'>(</font>window_table.get_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                et_signaler<font face='Lucida Console'>(</font>window_table.get_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                reference_to_global_mutex<font face='Lucida Console'>(</font>global_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                status <font color='#5555FF'>=</font> uninitialized;
            <b>}</b>

            <font color='#0000FF'><u>void</u></font> <b><a name='start_event_thread'></a>start_event_thread</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>
            <font color='#009900'>/*!
                we can't call this function from this objects constructor because 
                starting the event thread in windows involves sending messages to the
                WndProc() and that requires this object to be fully constructed.
            !*/</font>
            <b>{</b>

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>event_thread_started <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>event_thread_started <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        event_thread_started <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                        <font color='#009900'>// start up the event handler thread
</font>                        <font color='#BB00BB'>start</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                        <font color='#009900'>// wait for the event thread to get up and running
</font>                        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> uninitialized<font face='Lucida Console'>)</font>
                            et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> failure_to_init<font face='Lucida Console'>)</font>
                            <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Failed to start event thread</font>"<font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>
            <b>}</b>

            ~<b><a name='event_handler_thread'></a>event_handler_thread</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
                
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>is_alive</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font>helper_window,WM_USER<font color='#5555FF'>+</font>QUIT_EVENT_HANDLER_THREAD,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LWARN <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Unable to schedule function for execution in event handling thread.</font>";
                        <font color='#009900'>// No point calling wait() here since the thread isn't going to
</font>                        <font color='#009900'>// terminate gracefully in this case.  So we just let the program
</font>                        <font color='#009900'>// end as it will and hope for the best.
</font>                    <b>}</b> 
                    <font color='#0000FF'>else</font>
                    <b>{</b>
                        <font color='#009900'>// wait for the event handler thread to terminate.
</font>                        <font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>

            <b>}</b>

        <font color='#0000FF'>private</font>:

            <font color='#0000FF'><u>void</u></font> <b><a name='thread'></a>thread</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>
            <b>{</b>
                event_thread_id <font color='#5555FF'>=</font> <font color='#BB00BB'>get_thread_id</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                hInstance <font color='#5555FF'>=</font> <font color='#BB00BB'>GetModuleHandle</font><font face='Lucida Console'>(</font>NULL<font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hInstance <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                <b>{</b>
                    dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Error gathering needed resources</font>";

                    <font color='#009900'>// signal that an error has occurred
</font>                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    status <font color='#5555FF'>=</font> failure_to_init;
                    et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>return</font>;
                <b>}</b>

                <font color='#009900'>// register the main window class
</font>                WNDCLASS     wndclass ;

                wndclass.style         <font color='#5555FF'>=</font> CS_DBLCLKS;
                wndclass.lpfnWndProc   <font color='#5555FF'>=</font> dlib::gui_core_kernel_1_globals::WndProc ;
                wndclass.cbClsExtra    <font color='#5555FF'>=</font> <font color='#979000'>0</font> ;
                wndclass.cbWndExtra    <font color='#5555FF'>=</font> <font color='#979000'>0</font> ;
                wndclass.hInstance     <font color='#5555FF'>=</font> hInstance ;
                wndclass.hIcon         <font color='#5555FF'>=</font> <font color='#BB00BB'>LoadIcon</font> <font face='Lucida Console'>(</font>NULL, IDI_APPLICATION<font face='Lucida Console'>)</font> ;
                wndclass.hCursor       <font color='#5555FF'>=</font> <font color='#BB00BB'>LoadCursor</font> <font face='Lucida Console'>(</font>NULL, IDC_ARROW<font face='Lucida Console'>)</font> ;
                wndclass.hbrBackground <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                wndclass.lpszMenuName  <font color='#5555FF'>=</font> NULL ;
                wndclass.lpszClassName <font color='#5555FF'>=</font> window_class_name ;

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font><font color='#BB00BB'>RegisterClass</font> <font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>wndclass<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>  
                <b>{</b>
                    dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Error registering window class</font>";

                    <font color='#009900'>// signal that an error has occurred
</font>                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    status <font color='#5555FF'>=</font> failure_to_init;
                    et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>return</font>;
                <b>}</b>


                <font color='#009900'>// make the helper window that is used to trigger events in the
</font>                <font color='#009900'>// event handler loop from other threads
</font>                TCHAR nothing[] <font color='#5555FF'>=</font> <font color='#BB00BB'>TEXT</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
                helper_window <font color='#5555FF'>=</font> <font color='#BB00BB'>CreateWindow</font><font face='Lucida Console'>(</font>window_class_name,nothing,WS_DISABLED,<font color='#979000'>0</font>,<font color='#979000'>0</font>,<font color='#979000'>0</font>,<font color='#979000'>0</font>,HWND_MESSAGE,NULL,hInstance,NULL<font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>helper_window <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                <b>{</b>
                    dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Error gathering needed resources</font>";

                    <font color='#009900'>// signal that an error has occurred
</font>                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    status <font color='#5555FF'>=</font> failure_to_init;
                    et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>return</font>;
                <b>}</b>

                <font color='#009900'>// signal that the event thread is now up and running
</font>                window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                status <font color='#5555FF'>=</font> initialized;
                et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                <font color='#009900'>// start the event handler loop.   
</font>                <font color='#009900'>/*
                    A note about this quit_windows_loop thing.  If the user is holding 
                    the mouse button down on the title bar of a window it will cause
                    the PostQuitMessage() function to be ignored!!  This extra bool 
                    is a work around to prevent that from happening.
                */</font>
                MSG msg;
                <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>GetMessage</font> <font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>msg, NULL, <font color='#979000'>0</font>, <font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> 
                       quit_windows_loop <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#BB00BB'>TranslateMessage</font> <font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>msg<font face='Lucida Console'>)</font> ;
                    <font color='#BB00BB'>DispatchMessage</font> <font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>msg<font face='Lucida Console'>)</font> ;
                <b>}</b> 
            <b>}</b>
        <b>}</b>;

        <font color='#009900'>// Do all this just to make sure global_mutex() is initialized at program start
</font>        <font color='#009900'>// and thus hopefully before any threads have the chance to startup and call
</font>        <font color='#009900'>// global_data() concurrently.
</font>        <font color='#0000FF'>struct</font> <b><a name='call_global_mutex'></a>call_global_mutex</b> <b>{</b> <b><a name='call_global_mutex'></a>call_global_mutex</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#BB00BB'>global_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> <b>}</b>;
        <font color='#0000FF'>static</font> call_global_mutex call_global_mutex_instance;

        <font color='#0000FF'>const</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> <b><a name='global_data'></a>global_data</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
        <b>{</b>
            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#BB00BB'>global_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>static</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> p;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>p.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                p.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> <font color='#BB00BB'>event_handler_thread</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                M.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                p<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>start_event_thread</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <b>}</b>
            <font color='#0000FF'>return</font> p;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>struct</font> <b><a name='ebh_param'></a>ebh_param</b>
        <b>{</b>
            std::string text;
            std::string title;
        <b>}</b>;

        <font color='#0000FF'>static</font> <font color='#0000FF'><u>void</u></font> <b><a name='error_box_helper'></a>error_box_helper</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> param<font face='Lucida Console'>)</font>
        <b>{</b>
            ebh_param<font color='#5555FF'>&amp;</font> p <font color='#5555FF'>=</font> <font color='#5555FF'>*</font><font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font>ebh_param<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>param<font face='Lucida Console'>)</font>;
<font color='#0000FF'>#ifdef</font> UNICODE
            <font color='#BB00BB'>MessageBox</font> <font face='Lucida Console'>(</font>NULL,  <font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>p.text<font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, 
                        <font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>p.title<font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, MB_OK<font color='#5555FF'>|</font>MB_ICONERROR<font color='#5555FF'>|</font>MB_SYSTEMMODAL 
                        <font face='Lucida Console'>)</font>; 
<font color='#0000FF'>#else</font>
            <font color='#BB00BB'>MessageBox</font> <font face='Lucida Console'>(</font>NULL,  p.text.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, 
                        p.title.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, MB_OK<font color='#5555FF'>|</font>MB_ICONERROR<font color='#5555FF'>|</font>MB_SYSTEMMODAL 
                        <font face='Lucida Console'>)</font>; 
<font color='#0000FF'>#endif</font>
            <font color='#0000FF'>delete</font> <font color='#5555FF'>&amp;</font>p;
        <b>}</b>

        <font color='#0000FF'>static</font> <font color='#0000FF'><u>void</u></font> <b><a name='error_box'></a>error_box</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> title,
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> text,
            <font color='#0000FF'><u>bool</u></font> nonblocking <font color='#5555FF'>=</font> <font color='#979000'>false</font>
        <font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>try</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>nonblocking<font face='Lucida Console'>)</font>
                <b>{</b>
                    ebh_param<font color='#5555FF'>*</font> param <font color='#5555FF'>=</font> <font color='#0000FF'>new</font> ebh_param;
                    param<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>text <font color='#5555FF'>=</font> text;
                    param<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>title <font color='#5555FF'>=</font> title;
                    dlib::<font color='#BB00BB'>create_new_thread</font><font face='Lucida Console'>(</font>error_box_helper,param<font face='Lucida Console'>)</font>;
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
<font color='#0000FF'>#ifdef</font> UNICODE
                    <font color='#BB00BB'>MessageBox</font> <font face='Lucida Console'>(</font>NULL, <font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>text<font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, 
                                <font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>title<font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, 
                                MB_OK<font color='#5555FF'>|</font>MB_ICONERROR<font color='#5555FF'>|</font>MB_SYSTEMMODAL 
                                <font face='Lucida Console'>)</font>; 
<font color='#0000FF'>#else</font>
                    <font color='#BB00BB'>MessageBox</font> <font face='Lucida Console'>(</font>NULL,  text, 
                                title, MB_OK<font color='#5555FF'>|</font>MB_ICONERROR<font color='#5555FF'>|</font>MB_SYSTEMMODAL 
                                <font face='Lucida Console'>)</font>; 
<font color='#0000FF'>#endif</font>
                <b>}</b>
            <b>}</b>
            <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// we are totally screwed if this happens so just quit
</font>                <font color='#BB00BB'>exit</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>static</font> <font color='#0000FF'><u>bool</u></font> <b><a name='map_keys'></a>map_keys</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> keycode,
            <font color='#0000FF'><u>bool</u></font> shift,
            <font color='#0000FF'><u>bool</u></font> caps,
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> result,
            <font color='#0000FF'><u>bool</u></font><font color='#5555FF'>&amp;</font> is_printable
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            requires
                - if (shift was down for this key) then
                    - shift == true
                - if (caps lock was on for this key) then
                    - caps == true
                - keycode == the keycode from windows that we are to process
                - keycode &lt; keyboard_keys_size
            ensures
                - if (this key should be ignored) then
                    - returns false
                - else
                    - returns true
                    - #is_printable == true if result is a printable ascii character
                    - #result == the keycode converted into the proper number to tbe 
                      returned by the event handler.
        !*/</font>
        <b>{</b>
            is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;

            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>keycode <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>9</font>' <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> keycode <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>0</font>'<font face='Lucida Console'>)</font>
            <b>{</b>
                result <font color='#5555FF'>=</font> keycode;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>switch</font> <font face='Lucida Console'>(</font>result<font face='Lucida Console'>)</font>
                    <b>{</b>
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>0</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>)</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>1</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>!</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>2</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>@</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>3</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>#</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>4</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>$</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>5</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>%</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>6</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>^</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>7</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>&amp;</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>8</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>*</font>'; <font color='#0000FF'>break</font>;
                    <font color='#0000FF'>case</font> '<font color='#FF0000'>9</font>': result <font color='#5555FF'>=</font> '<font color='#FF0000'>(</font>'; <font color='#0000FF'>break</font>;
                    <b>}</b>
                <b>}</b>
            <b>}</b>
            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>keycode <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>Z</font>' <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> keycode <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>A</font>'<font face='Lucida Console'>)</font>
            <b>{</b>
                result <font color='#5555FF'>=</font> keycode;

                <font color='#009900'>// make the result lower case if we need to.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>shift <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> caps<font face='Lucida Console'>)</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>caps <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> <font color='#5555FF'>!</font>shift<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    result <font color='#5555FF'>=</font> result <font color='#5555FF'>-</font> '<font color='#FF0000'>A</font>' <font color='#5555FF'>+</font> '<font color='#FF0000'>a</font>';               
            <b>}</b>
            <font color='#0000FF'>else</font>
            <b>{</b>
                <font color='#0000FF'>switch</font> <font face='Lucida Console'>(</font>keycode<font face='Lucida Console'>)</font>
                <b>{</b>
                <font color='#0000FF'>case</font> VK_BACK:   
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_BACKSPACE; 
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_SHIFT:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_SHIFT;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_CONTROL:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_CTRL;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_MENU:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_ALT;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_PAUSE:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_PAUSE;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_CAPITAL:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_CAPS_LOCK;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_ESCAPE:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_ESC;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_PRIOR:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_PAGE_UP;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_NEXT:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_PAGE_DOWN;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_END:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_END;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_HOME:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_HOME;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_LEFT:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_LEFT;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_RIGHT:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_RIGHT;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_UP:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_UP;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_DOWN:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_DOWN;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_INSERT:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_INSERT;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_DELETE:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_DELETE;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> <font color='#979000'>0x91</font>:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_SCROLL_LOCK;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F1:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F1;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F2:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F2;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F3:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F3;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F4:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F4;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F5:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F5;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F6:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F6;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F7:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F7;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F8:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F8;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F9:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F9;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F10:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F10;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F11:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F11;
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_F12:
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                    result <font color='#5555FF'>=</font> base_window::KEY_F12;
                    <font color='#0000FF'>break</font>;
      

                <font color='#0000FF'>case</font> VK_SPACE:  result <font color='#5555FF'>=</font> '<font color='#FF0000'> </font>';  <font color='#0000FF'>break</font>;                
                <font color='#0000FF'>case</font> VK_TAB:    result <font color='#5555FF'>=</font> '<font color='#FF0000'>\t</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_RETURN: result <font color='#5555FF'>=</font> '<font color='#FF0000'>\n</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD0:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>0</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD1:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>1</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD2:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>2</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD3:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>3</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD4:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>4</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD5:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>5</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD6:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>6</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD7:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>7</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD8:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>8</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_NUMPAD9:  result <font color='#5555FF'>=</font> '<font color='#FF0000'>9</font>';  <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_MULTIPLY:   result <font color='#5555FF'>=</font> '<font color='#FF0000'>*</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_ADD:        result <font color='#5555FF'>=</font> '<font color='#FF0000'>+</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_SUBTRACT:   result <font color='#5555FF'>=</font> '<font color='#FF0000'>-</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_DECIMAL:    result <font color='#5555FF'>=</font> '<font color='#FF0000'>.</font>';  <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> VK_DIVIDE:     result <font color='#5555FF'>=</font> '<font color='#FF0000'>/</font>';  <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_1:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>:</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>;</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_PLUS:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>+</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>=</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_COMMA:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>&lt;</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>,</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_MINUS:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>_</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>-</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_PERIOD:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>&gt;</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>.</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_2:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>?</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>/</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_3:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>~</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>`</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_4:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>{</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>[</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_5:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>|</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>\\</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_6:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>}</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>]</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> VK_OEM_7:
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>  result <font color='#5555FF'>=</font> '<font color='#FF0000'>"</font>';
                    <font color='#0000FF'>else</font>        result <font color='#5555FF'>=</font> '<font color='#FF0000'>\'</font>';
                    <font color='#0000FF'>break</font>;

                <font color='#0000FF'>default</font>:
                    <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
                <b>}</b>
            <b>}</b>

            <font color='#0000FF'>return</font> <font color='#979000'>true</font>;
        <b>}</b>

    <font color='#009900'>// ------------------------------------------------------------------------------------
</font>
        LRESULT CALLBACK <b><a name='WndProc'></a>WndProc</b> <font face='Lucida Console'>(</font>  
            HWND hwnd, 
            UINT message, 
            WPARAM wParam, 
            LPARAM lParam
        <font face='Lucida Console'>)</font>
        <b>{</b>        
            <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
            <font color='#009900'>// Make the event processing thread have a priority slightly above normal.
</font>            <font color='#009900'>// This makes the GUI smother if you do heavy processing in other threads.
</font>            HANDLE hand <font color='#5555FF'>=</font> <font color='#BB00BB'>OpenThread</font><font face='Lucida Console'>(</font>THREAD_ALL_ACCESS,FALSE,<font color='#BB00BB'>GetCurrentThreadId</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>SetThreadPriority</font><font face='Lucida Console'>(</font>hand,THREAD_PRIORITY_ABOVE_NORMAL<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>CloseHandle</font><font face='Lucida Console'>(</font>hand<font face='Lucida Console'>)</font>;

            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

            window_table_type<font color='#5555FF'>&amp;</font> window_table <font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table;
            HWND<font color='#5555FF'>&amp;</font> helper_window <font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window;

            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

            <font color='#0000FF'>try</font>
            <b>{</b>
                std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font> bitmap_buffer;

                <font color='#0000FF'><u>bool</u></font> is_double <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> btn <font color='#5555FF'>=</font> base_window::NONE;

                <font color='#0000FF'>switch</font> <font face='Lucida Console'>(</font>message<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>QUIT_EVENT_HANDLER_THREAD:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                            
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>quit_windows_loop <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                            <font color='#BB00BB'>PostQuitMessage</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>; 
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>DESTROY_WINDOW:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                            
                            <font color='#BB00BB'>DestroyWindow</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>HWND<font face='Lucida Console'>)</font>wParam<font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>CALL_MOVE_WINDOW:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>
                            <font color='#BB00BB'>MoveWindow</font><font face='Lucida Console'>(</font>
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_hwnd,
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_x,
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_y,
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_width,
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_height,
                                TRUE<font face='Lucida Console'>)</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_done <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>USER_EVENTS_READY:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>
                            <font color='#009900'>// this is the signal to look in the user_events queue 
</font>                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp<font face='Lucida Console'>)</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            <font color='#009900'>// now dispatch all these user events
</font>                            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.w];
                                base_window<font color='#5555FF'>*</font> win;
                                <font color='#009900'>// if this window exists in the window table then dispatch
</font>                                <font color='#009900'>// its event.
</font>                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_user_event</font><font face='Lucida Console'>(</font>
                                        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.p,
                                        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.i
                                    <font face='Lucida Console'>)</font>;
                                <b>}</b>
                            <b>}</b>
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>SET_ACTIVE_WINDOW:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                            
                            <font color='#BB00BB'>SetActiveWindow</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>HWND<font face='Lucida Console'>)</font>wParam<font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>SHOW_WINDOW_SHOW:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                            
                            <font color='#BB00BB'>ShowWindow</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>HWND<font face='Lucida Console'>)</font>wParam,SW_SHOW<font face='Lucida Console'>)</font>;
                            <font color='#BB00BB'>BringWindowToTop</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>HWND<font face='Lucida Console'>)</font>wParam<font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>SHOW_WINDOW_HIDE:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                            
                            <font color='#BB00BB'>ShowWindow</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>HWND<font face='Lucida Console'>)</font>wParam,SW_HIDE<font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>CALL_SET_WINDOW_TITLE:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                            
                            <font color='#BB00BB'>SetWindowTextW</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>HWND<font face='Lucida Console'>)</font>wParam,globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_title.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>set_window_title_done <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;


                    <font color='#0000FF'>case</font> WM_USER<font color='#5555FF'>+</font>CREATE_WINDOW:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> helper_window<font face='Lucida Console'>)</font>
                        <b>{</b>                 

                            <font color='#009900'>// if this is stupposed to be a popup window then do the popup window thing
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>dwStyle <font color='#5555FF'>=</font><font color='#5555FF'>=</font> WS_CHILD<font face='Lucida Console'>)</font>
                            <b>{</b>
                                TCHAR nothing[] <font color='#5555FF'>=</font> <font color='#BB00BB'>TEXT</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window <font color='#5555FF'>=</font> <font color='#BB00BB'>CreateWindowEx</font> <font face='Lucida Console'>(</font>WS_EX_TOOLWINDOW,globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_class_name, nothing,
                                    globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>dwStyle,
                                    CW_USEDEFAULT, CW_USEDEFAULT,
                                    CW_USEDEFAULT, CW_USEDEFAULT,
                                    helper_window, NULL, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>hInstance, NULL<font face='Lucida Console'>)</font>;
                                <font color='#BB00BB'>SetParent</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window,NULL<font face='Lucida Console'>)</font>;
                            <b>}</b>
                            <font color='#0000FF'>else</font>
                            <b>{</b>
                                TCHAR nothing[] <font color='#5555FF'>=</font> <font color='#BB00BB'>TEXT</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window <font color='#5555FF'>=</font> <font color='#BB00BB'>CreateWindow</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_class_name, nothing,
                                    globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>dwStyle,
                                    CW_USEDEFAULT, CW_USEDEFAULT,
                                    CW_USEDEFAULT, CW_USEDEFAULT,
                                    NULL, NULL, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>hInstance, NULL<font face='Lucida Console'>)</font>;
                            <b>}</b>
                            <font color='#009900'>// use the helper_window to indicate that CreateWindow failed
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window <font color='#5555FF'>=</font> helper_window;
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_SYSKEYDOWN:
                    <font color='#0000FF'>case</font> WM_KEYDOWN:
                        <b>{</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>in_ime_composition<font face='Lucida Console'>)</font> <font color='#0000FF'>break</font>;

                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

                            <font color='#0000FF'><u>bool</u></font> shift <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GetKeyState</font><font face='Lucida Console'>(</font>VK_SHIFT<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0x8000</font><font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'><u>bool</u></font> ctrl <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GetKeyState</font><font face='Lucida Console'>(</font>VK_CONTROL<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0x8000</font><font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'><u>bool</u></font> caps <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GetKeyState</font><font face='Lucida Console'>(</font>VK_CAPITAL<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0x0001</font><font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>=</font> base_window::KBD_MOD_SHIFT;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>ctrl<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_CONTROL;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>caps<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_CAPS_LOCK;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GetKeyState</font><font face='Lucida Console'>(</font>VK_MENU<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0x8000</font><font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_ALT;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GetKeyState</font><font face='Lucida Console'>(</font>VK_NUMLOCK<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0x0001</font><font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_NUM_LOCK;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GetKeyState</font><font face='Lucida Console'>(</font>VK_SCROLL<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0x0001</font><font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_SCROLL_LOCK;


                            <font color='#0000FF'><u>bool</u></font> is_printable;
                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> result;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>map_keys</font><font face='Lucida Console'>(</font>wParam,shift,caps,result,is_printable<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// signal the keyboard event
</font>                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_keydown</font><font face='Lucida Console'>(</font>result,is_printable,state<font face='Lucida Console'>)</font>;
                            <b>}</b>
                           
                        <b>}</b>
                        <font color='#0000FF'>break</font>;

                        <font color='#009900'>// treat the user releasing the mouse button on the non client area (e.g. the title bar)
</font>                        <font color='#009900'>// like focus being lost since that is what X11 does
</font>                    <font color='#0000FF'>case</font> WM_NCLBUTTONUP:
                    <font color='#0000FF'>case</font> WM_NCMBUTTONUP:
                    <font color='#0000FF'>case</font> WM_NCRBUTTONUP:
                    <font color='#0000FF'>case</font> WM_SETFOCUS:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#009900'>// signal that the window is gaining focus 
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_focus_gained</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>break</font>;

                        <font color='#009900'>// treat the user clicking on the non client area (e.g. the title bar)
</font>                        <font color='#009900'>// like focus being lost since that is what X11 does
</font>                    <font color='#0000FF'>case</font> WM_NCLBUTTONDBLCLK:
                    <font color='#0000FF'>case</font> WM_NCMBUTTONDBLCLK:
                    <font color='#0000FF'>case</font> WM_NCRBUTTONDBLCLK:
                    <font color='#0000FF'>case</font> WM_NCLBUTTONDOWN:
                    <font color='#0000FF'>case</font> WM_NCMBUTTONDOWN:
                    <font color='#0000FF'>case</font> WM_NCRBUTTONDOWN:
                    <font color='#0000FF'>case</font> WM_KILLFOCUS:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#009900'>// signal that the window is gaining focus 
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_focus_lost</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> WM_SIZE:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;


                            <font color='#009900'>// signal that the window has been resized
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_resized</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                           
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_MOVE:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;


                            <font color='#009900'>// signal that the window has moved 
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_moved</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                           
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_MOUSELEAVE:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;


                            <font color='#009900'>// signal that the mouse has left the window
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in<font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_leave</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                            <b>}</b>
                           
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_MOUSEWHEEL:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_CONTROL<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::CONTROL;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_LBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::LEFT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_MBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::MIDDLE;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_RBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::RIGHT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_SHIFT<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::SHIFT;

                            <font color='#009900'>// signal the mouse wheel event
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>GET_WHEEL_DELTA_WPARAM</font><font face='Lucida Console'>(</font>wParam<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_wheel_up</font><font face='Lucida Console'>(</font>state<font face='Lucida Console'>)</font>;                                
                            <b>}</b>
                            <font color='#0000FF'>else</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_wheel_down</font><font face='Lucida Console'>(</font>state<font face='Lucida Console'>)</font>;
                            <b>}</b>
                           
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_LBUTTONUP:
                        btn <font color='#5555FF'>=</font> base_window::LEFT;
                    <font color='#0000FF'>case</font> WM_MBUTTONUP:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::MIDDLE;
                    <font color='#0000FF'>case</font> WM_RBUTTONUP:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::RIGHT;
                        <b>{</b>        
                            <font color='#009900'>// release the mouse capture if the user isn't holding any
</font>                            <font color='#009900'>// other mouse buttons
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_LBUTTON<font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_MBUTTON<font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_RBUTTON<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                                <font color='#BB00BB'>ReleaseCapture</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_CONTROL<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::CONTROL;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_LBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::LEFT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_MBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::MIDDLE;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_RBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::RIGHT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_SHIFT<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::SHIFT;
                            
                            <font color='#009900'>// remove the clicked button from the state
</font>                            state <font color='#5555FF'>&amp;</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>~btn<font face='Lucida Console'>)</font>;

                            <font color='#009900'>// signal the mouse click
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_up</font><font face='Lucida Console'>(</font>btn,state,<font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>,<font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                           
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;



                    <font color='#0000FF'>case</font> WM_LBUTTONDBLCLK:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::LEFT;
                    <font color='#0000FF'>case</font> WM_MBUTTONDBLCLK:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::MIDDLE;
                    <font color='#0000FF'>case</font> WM_RBUTTONDBLCLK:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::RIGHT;
                        is_double <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    <font color='#0000FF'>case</font> WM_LBUTTONDOWN:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::LEFT;
                    <font color='#0000FF'>case</font> WM_MBUTTONDOWN:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::MIDDLE;
                    <font color='#0000FF'>case</font> WM_RBUTTONDOWN:
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::NONE<font face='Lucida Console'>)</font>
                            btn <font color='#5555FF'>=</font> base_window::RIGHT;
                        <b>{</b>
                            <font color='#BB00BB'>SetCapture</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;


                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;
                            
                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_CONTROL<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::CONTROL;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_LBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::LEFT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_MBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::MIDDLE;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_RBUTTON<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::RIGHT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_SHIFT<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::SHIFT;

                            <font color='#009900'>// remove the clicked button from the state
</font>                            state <font color='#5555FF'>&amp;</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>~btn<font face='Lucida Console'>)</font>;

                            <font color='#009900'>// signal the mouse click
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_down</font><font face='Lucida Console'>(</font>btn,state,<font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>,<font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>,is_double<font face='Lucida Console'>)</font>;
                           
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_MOUSEMOVE:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;
                            
                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'><u>bool</u></font> mouse_button_down <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_CONTROL<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::CONTROL;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_LBUTTON<font face='Lucida Console'>)</font>
                            <b>{</b>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::LEFT;
                                mouse_button_down <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                            <b>}</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_MBUTTON<font face='Lucida Console'>)</font>
                            <b>{</b>
                                mouse_button_down <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::MIDDLE;
                            <b>}</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_RBUTTON<font face='Lucida Console'>)</font>
                            <b>{</b>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::RIGHT;
                                mouse_button_down <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                            <b>}</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>wParam <font color='#5555FF'>&amp;</font> MK_SHIFT<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::SHIFT;

                            <font color='#009900'>// signal the mouse movement if this mouse event isn't identical to the
</font>                            <font color='#009900'>// last one we got
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> <font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>prevx <font color='#5555FF'>|</font><font color='#5555FF'>|</font> 
                                 <font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>prevy <font color='#5555FF'>|</font><font color='#5555FF'>|</font> 
                                 state <font color='#5555FF'>!</font><font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>prev_state<font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_move</font><font face='Lucida Console'>(</font>state,<font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>,<font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                            <b>}</b>
                           
                            <font color='#009900'>// save the event data into the prev* member variables
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>prevx <font color='#5555FF'>=</font> <font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>;
                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>prevy <font color='#5555FF'>=</font> <font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>;
                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>prev_state <font color='#5555FF'>=</font> state;

                            <font color='#009900'>// The following block of code checks if the mouse is moving
</font>                            <font color='#009900'>// into or out of the window.
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>mouse_button_down <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// if there isn't any mouse button down then the fact that
</font>                                <font color='#009900'>// we are getting a mouse move message means it is in the
</font>                                <font color='#009900'>// window
</font>                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_enter</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font> <font color='#979000'>true</font>;

                                    <font color='#009900'>// set the tracker for the mouse
</font>                                    TRACKMOUSEEVENT tm;
                                    tm.hwndTrack <font color='#5555FF'>=</font> hwnd;
                                    tm.cbSize <font color='#5555FF'>=</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>tm<font face='Lucida Console'>)</font>;
                                    tm.dwFlags <font color='#5555FF'>=</font> TME_LEAVE;
                                    <font color='#BB00BB'>_TrackMouseEvent</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>tm<font face='Lucida Console'>)</font>;
                                <b>}</b>
                            <b>}</b>
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// check if the mouse is currently outside the window
</font>                                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> mouse_x <font color='#5555FF'>=</font> <font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> mouse_y <font color='#5555FF'>=</font> <font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>mouse_x <font color='#5555FF'>&lt;</font> <font color='#979000'>0</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> mouse_y <font color='#5555FF'>&lt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    <font color='#009900'>// the mouse is not in the window
</font>                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_leave</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                <b>}</b>
                                <font color='#0000FF'>else</font>
                                <b>{</b>
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> width, height;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_size</font><font face='Lucida Console'>(</font>width,height<font face='Lucida Console'>)</font>;  
                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>mouse_x <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>width<font face='Lucida Console'>)</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> 
                                        mouse_y <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>height<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        <font color='#009900'>// the mouse is not in the window
</font>                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_leave</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                    <b>}</b>
                                <b>}</b>
                            <b>}</b>
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// at this point we know that the mouse is moving around
</font>                                <font color='#009900'>// with some of its buttons down.  So it might be outside the window.
</font>                                <font color='#009900'>// get the window size and see if the mouse is outside
</font>                                <font color='#009900'>// it.
</font>                                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> mouse_x <font color='#5555FF'>=</font> <font color='#BB00BB'>GET_X_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> mouse_y <font color='#5555FF'>=</font> <font color='#BB00BB'>GET_Y_LPARAM</font><font face='Lucida Console'>(</font>lParam<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> width, height;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>get_size</font><font face='Lucida Console'>(</font>width,height<font face='Lucida Console'>)</font>;  
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>mouse_x <font color='#5555FF'>&lt;</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>width<font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> 
                                    mouse_y <font color='#5555FF'>&lt;</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>height<font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                                    mouse_x <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                                    mouse_y <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    <font color='#009900'>// The mouse has gone inside the window
</font>                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>mouse_in <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_enter</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                                    <font color='#009900'>// set the tracker for the mouse
</font>                                    TRACKMOUSEEVENT tm;
                                    tm.hwndTrack <font color='#5555FF'>=</font> hwnd;
                                    tm.cbSize <font color='#5555FF'>=</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>tm<font face='Lucida Console'>)</font>;
                                    tm.dwFlags <font color='#5555FF'>=</font> TME_LEAVE;
                                    <font color='#BB00BB'>_TrackMouseEvent</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>tm<font face='Lucida Console'>)</font>;
                                <b>}</b>
                               
                            <b>}</b>


                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

                    <font color='#0000FF'>case</font> WM_PAINT :
                        <b>{</b>     

                            PAINTSTRUCT ps;
                            HDC   hdc <font color='#5555FF'>=</font> NULL;

                            hdc <font color='#5555FF'>=</font> <font color='#BB00BB'>BeginPaint</font> <font face='Lucida Console'>(</font>hwnd, <font color='#5555FF'>&amp;</font>ps<font face='Lucida Console'>)</font> ;

                            <font color='#0000FF'>try</font>
                            <b>{</b>
                                base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                                base_window<font color='#5555FF'>*</font> win;
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                    win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                                <font color='#0000FF'>else</font>
                                    <font color='#0000FF'>break</font>;




                                LONG x <font color='#5555FF'>=</font> ps.rcPaint.left;
                                LONG y <font color='#5555FF'>=</font> ps.rcPaint.top;
                                LONG width <font color='#5555FF'>=</font> ps.rcPaint.right <font color='#5555FF'>-</font> x;
                                LONG height <font color='#5555FF'>=</font> ps.rcPaint.bottom <font color='#5555FF'>-</font> y;
                        
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>width <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> height <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <b>{</b>

                                    BITMAPINFO bmap_info;
                                    bmap_info.bmiColors[<font color='#979000'>0</font>].rgbBlue <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiColors[<font color='#979000'>0</font>].rgbGreen <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiColors[<font color='#979000'>0</font>].rgbRed <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiColors[<font color='#979000'>0</font>].rgbReserved <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiHeader.biSize <font color='#5555FF'>=</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>bmap_info.bmiHeader<font face='Lucida Console'>)</font>;
                                    bmap_info.bmiHeader.biWidth <font color='#5555FF'>=</font> width;
                                    bmap_info.bmiHeader.biHeight <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>1</font><font color='#5555FF'>*</font>height;
                                    bmap_info.bmiHeader.biPlanes <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
                                    bmap_info.bmiHeader.biBitCount <font color='#5555FF'>=</font> <font color='#979000'>24</font>;
                                    bmap_info.bmiHeader.biCompression <font color='#5555FF'>=</font> BI_RGB;
                                    bmap_info.bmiHeader.biSizeImage <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiHeader.biXPelsPerMeter <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiHeader.biYPelsPerMeter <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiHeader.biClrUsed <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    bmap_info.bmiHeader.biClrImportant <font color='#5555FF'>=</font> <font color='#979000'>0</font>;



                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> bitmap ;
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> size;
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> padding <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>width<font color='#5555FF'>*</font><font color='#979000'>3</font><font face='Lucida Console'>)</font><font color='#5555FF'>%</font><font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>LONG<font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        padding <font color='#5555FF'>=</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>LONG<font face='Lucida Console'>)</font> <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font>width<font color='#5555FF'>*</font><font color='#979000'>3</font><font face='Lucida Console'>)</font><font color='#5555FF'>%</font><font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>LONG<font face='Lucida Console'>)</font>;
                                        size <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>width<font color='#5555FF'>*</font><font color='#979000'>3</font><font color='#5555FF'>+</font>padding<font face='Lucida Console'>)</font><font color='#5555FF'>*</font>height;
                                    <b>}</b>
                                    <font color='#0000FF'>else</font>
                                    <b>{</b>
                                        size <font color='#5555FF'>=</font> width<font color='#5555FF'>*</font>height<font color='#5555FF'>*</font><font color='#979000'>3</font>;
                                    <b>}</b>

                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>bitmap_buffer.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font> size<font face='Lucida Console'>)</font>
                                        bitmap_buffer.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>size<font face='Lucida Console'>)</font>;
                                    bitmap <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>bitmap_buffer[<font color='#979000'>0</font>];                         

                                    canvas <font color='#BB00BB'>bits</font><font face='Lucida Console'>(</font>bitmap,padding,x,y,x<font color='#5555FF'>+</font>width<font color='#5555FF'>-</font><font color='#979000'>1</font>,y<font color='#5555FF'>+</font>height<font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;



                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>paint</font><font face='Lucida Console'>(</font>bits<font face='Lucida Console'>)</font>;


                                    
                                    <font color='#BB00BB'>SetDIBitsToDevice</font> <font face='Lucida Console'>(</font>
                                        hdc,
                                        ps.rcPaint.left,
                                        ps.rcPaint.top,
                                        width,
                                        height,
                                        <font color='#979000'>0</font>,
                                        <font color='#979000'>0</font>,
                                        <font color='#979000'>0</font>,
                                        height,
                                        bitmap,
                                        <font color='#5555FF'>&amp;</font>bmap_info,
                                        DIB_RGB_COLORS
                                        <font face='Lucida Console'>)</font>;
                                <b>}</b>

                                <font color='#BB00BB'>EndPaint</font> <font face='Lucida Console'>(</font>hwnd, <font color='#5555FF'>&amp;</font>ps<font face='Lucida Console'>)</font> ;    

                            <b>}</b>
                            <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// make sure EndPaint is called even if an exception
</font>                                <font color='#009900'>// is thrown.
</font>                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hdc <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                                    <font color='#BB00BB'>EndPaint</font> <font face='Lucida Console'>(</font>hwnd, <font color='#5555FF'>&amp;</font>ps<font face='Lucida Console'>)</font>;    
                                <font color='#0000FF'>throw</font>;
                            <b>}</b>
                        <b>}</b>   
                        <font color='#0000FF'>return</font> <font color='#979000'>0</font> ;

                    <font color='#0000FF'>case</font>  WM_ERASEBKGND:
                        <font color='#0000FF'>return</font> <font color='#979000'>1</font>;




                    <font color='#0000FF'>case</font> WM_CLOSE:
                        <b>{</b>
                            base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                            base_window<font color='#5555FF'>*</font> win;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                                win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                            <font color='#0000FF'>else</font>
                                <font color='#0000FF'>break</font>;
                            
  
                            <font color='#009900'>// signal that the window is being closed                                
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_close</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::DO_NOT_CLOSE_WINDOW<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font>,
                                    "<font color='#CC0000'>\tYou called close_window() inside the on_window_close() event but</font>" 
                                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthen returned DO_NOT_CLOSE_WINDOW.  You can do one or the other but not both.</font>"
                                    <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> win 
                                    <font face='Lucida Console'>)</font>;
                                <font color='#009900'>// this happens if the on_window_close() callback
</font>                                <font color='#009900'>// tells us to ignore the close event.  
</font>                                <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
                            <b>}</b>
                            <font color='#0000FF'>else</font>
                            <b>{</b>
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>window_table[hwnd]<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    window_table.<font color='#BB00BB'>destroy</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_destroyed <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>hwnd <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                    globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_close_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                <b>}</b>
                                <font color='#0000FF'>else</font>
                                <b>{</b>
                                    <font color='#009900'>// in this case the window must have self destructed by
</font>                                    <font color='#009900'>// calling delete this;
</font>                                    <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
                                <b>}</b>
                            <b>}</b>
                          
                        <b>}</b>
                        <font color='#0000FF'>return</font> <font color='#BB00BB'>DefWindowProc</font> <font face='Lucida Console'>(</font>hwnd, message, wParam, lParam<font face='Lucida Console'>)</font>;

                    <font color='#0000FF'>case</font> WM_IME_STARTCOMPOSITION:
                        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>in_ime_composition <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                        <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> WM_IME_COMPOSITION:
                        <b>{</b>
                        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>in_ime_composition <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                        base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[hwnd];
                        base_window<font color='#5555FF'>*</font> win;
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                            win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                        <font color='#0000FF'>else</font>
                            <font color='#0000FF'>break</font>;
                        HIMC hImc <font color='#5555FF'>=</font> <font color='#BB00BB'>ImmGetContext</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>lParam <font color='#5555FF'>&amp;</font> GCS_RESULTSTR<font face='Lucida Console'>)</font><b>{</b>
                            WCHAR wc;
                            LONG bufbyte <font color='#5555FF'>=</font> <font color='#BB00BB'>ImmGetCompositionStringW</font><font face='Lucida Console'>(</font>hImc, GCS_RESULTSTR, <font color='#5555FF'>&amp;</font>wc, <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>bufbyte <font color='#5555FF'>!</font><font color='#5555FF'>=</font> IMM_ERROR_NODATA <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> bufbyte <font color='#5555FF'>!</font><font color='#5555FF'>=</font> IMM_ERROR_GENERAL<font face='Lucida Console'>)</font><b>{</b>
                                bufbyte <font color='#5555FF'>+</font><font color='#5555FF'>=</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>WCHAR<font face='Lucida Console'>)</font>;


                                WCHAR <font color='#5555FF'>*</font>buf <font color='#5555FF'>=</font> <font color='#0000FF'>new</font> WCHAR[bufbyte <font color='#5555FF'>/</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>WCHAR<font face='Lucida Console'>)</font>];
                                <font color='#BB00BB'>ImmGetCompositionStringW</font><font face='Lucida Console'>(</font>hImc, GCS_RESULTSTR, buf, bufbyte<font face='Lucida Console'>)</font>;
                                buf[bufbyte <font color='#5555FF'>/</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>WCHAR<font face='Lucida Console'>)</font> <font color='#5555FF'>-</font> <font color='#979000'>1</font>] <font color='#5555FF'>=</font> L'<font color='#FF0000'>\0</font>';

                                <font color='#009900'>// signal the putstring event
</font>                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_string_put</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>wstring</font><font face='Lucida Console'>(</font>buf<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>delete</font> [] buf;
                            <b>}</b>
                        <b>}</b>
                        <font color='#BB00BB'>ImmReleaseContext</font><font face='Lucida Console'>(</font>hwnd, hImc<font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>default</font>:
                        <font color='#0000FF'>break</font>;

                <b>}</b> <font color='#009900'>// switch (message)
</font>
            
            <b>}</b>
            <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>std::exception<font color='#5555FF'>&amp;</font> e<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#BB00BB'>error_box</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Exception thrown in event handler</font>",e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>quit_windows_loop <font color='#5555FF'>=</font> <font color='#979000'>true</font>; 
            <b>}</b>
            <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#BB00BB'>error_box</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Exception thrown in event handler</font>","<font color='#CC0000'>Unknown Exception type.</font>"<font face='Lucida Console'>)</font>;
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>quit_windows_loop <font color='#5555FF'>=</font> <font color='#979000'>true</font>; 
            <b>}</b>

            <font color='#0000FF'>return</font> <font color='#BB00BB'>DefWindowProc</font> <font face='Lucida Console'>(</font>hwnd, message, wParam, lParam<font face='Lucida Console'>)</font> ;

        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'><u>void</u></font> <b><a name='show_window'></a>show_window</b> <font face='Lucida Console'>(</font>
            HWND hwnd
        <font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
            <font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>SHOW_WINDOW_SHOW,<font face='Lucida Console'>(</font>WPARAM<font face='Lucida Console'>)</font>hwnd,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'><u>void</u></font> <b><a name='hide_window'></a>hide_window</b> <font face='Lucida Console'>(</font>
            HWND hwnd
        <font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
            <font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>SHOW_WINDOW_HIDE,<font face='Lucida Console'>(</font>WPARAM<font face='Lucida Console'>)</font>hwnd,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'><u>void</u></font> <b><a name='give_window_focus'></a>give_window_focus</b> <font face='Lucida Console'>(</font>
            HWND hwnd
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - calls SetActiveWindow(hwnd) from the event handling thread.
        !*/</font>
        <b>{</b>
            <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
            <font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>SET_ACTIVE_WINDOW,<font face='Lucida Console'>(</font>WPARAM<font face='Lucida Console'>)</font>hwnd,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'><u>void</u></font> <b><a name='destroy_window'></a>destroy_window</b> <font face='Lucida Console'>(</font>
            HWND hwnd
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - calls DestroyWindow(hwnd) from the event handling thread.  
        !*/</font>
        <b>{</b>
            <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
            <font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>DESTROY_WINDOW,<font face='Lucida Console'>(</font>WPARAM<font face='Lucida Console'>)</font>hwnd,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        HWND <b><a name='make_window'></a>make_window</b> <font face='Lucida Console'>(</font>
            DWORD dwStyle_
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - creates a window by calling CreateWindow and passes on the 
                  dwStyle argument.  
                - returns the HWND that is returned by CreateWindow
                - ensures that CreateWindow is called from the event handler thread
                - if (it was unable to create a window) then
                    - returns NULL or helper_window
        !*/</font>
        <b>{</b>   
            <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#009900'>// if we are running in the event handling thread then just call
</font>            <font color='#009900'>// CreateWindow directly
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>get_thread_id</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>event_thread_id<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// if this is stupposed to be a popup window then do the popup window thing
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>dwStyle_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> WS_CHILD<font face='Lucida Console'>)</font>
                <b>{</b>
                    TCHAR nothing[] <font color='#5555FF'>=</font> <font color='#BB00BB'>TEXT</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
                    HWND tmp <font color='#5555FF'>=</font> <font color='#BB00BB'>CreateWindowEx</font> <font face='Lucida Console'>(</font>WS_EX_TOOLWINDOW<font color='#5555FF'>|</font>WS_EX_TOPMOST, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_class_name, nothing,
                            dwStyle_,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window, NULL, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>hInstance, NULL<font face='Lucida Console'>)</font>;
                    <font color='#BB00BB'>SetParent</font><font face='Lucida Console'>(</font>tmp,NULL<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>return</font> tmp;
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
                    TCHAR nothing[] <font color='#5555FF'>=</font> <font color='#BB00BB'>TEXT</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>return</font> <font color='#BB00BB'>CreateWindow</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_class_name, nothing,
                            dwStyle_,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>hInstance, NULL<font face='Lucida Console'>)</font>;
                <b>}</b>
            <b>}</b>
            <font color='#0000FF'>else</font>
            <b>{</b>
                auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <font color='#009900'>// wait for our chance to make a new window request
</font>                <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>request_new_window<font face='Lucida Console'>)</font>
                    globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;


                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>dwStyle <font color='#5555FF'>=</font> dwStyle_;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>CREATE_WINDOW,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to schedule function for execution in event handling thread.</font>"<font face='Lucida Console'>)</font>;
                <b>}</b> 

                <font color='#009900'>// wait for our request to be serviced
</font>                <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                    globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                HWND temp <font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window;
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>new_window <font color='#5555FF'>=</font> NULL;
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>request_new_window <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                <font color='#009900'>// if make_window() returns the helper_window then it means it failed
</font>                <font color='#009900'>// to make a new window
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>=</font><font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window<font face='Lucida Console'>)</font>
                    temp <font color='#5555FF'>=</font> NULL;

                <font color='#0000FF'>return</font> temp;
            <b>}</b>
        <b>}</b>

    <font color='#009900'>// ------------------------------------------------------------------------------------
</font>

    <b>}</b> <font color='#009900'>// end namespace gui_core_kernel_1_globals
</font>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> canvas::
    <b><a name='fill'></a>fill</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> red_,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> green_,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> blue_
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> red <font color='#5555FF'>=</font> red_;
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> green <font color='#5555FF'>=</font> green_;
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> blue <font color='#5555FF'>=</font> blue_;

        <font color='#0000FF'>const</font> LONG block1 <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>blue<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>24</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>red<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>16</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>green<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> blue;
        <font color='#0000FF'>const</font> LONG block2 <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>green<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>24</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>blue<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>16</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>red<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> green;
        <font color='#0000FF'>const</font> LONG block3 <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>red<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>24</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>green<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>16</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>blue<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font> <font color='#5555FF'>|</font> red;

        <font color='#009900'>// remember that row_width is a multiple of 4 because windows
</font>        <font color='#009900'>// requires that all bitmaps have row widths that are multiples of 4.
</font>        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> size <font color='#5555FF'>=</font> row_width<font color='#5555FF'>/</font><font color='#979000'>4</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> height_; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> padding <font color='#5555FF'>=</font> size<font color='#5555FF'>%</font><font color='#979000'>3</font>;                
            LONG<font color='#5555FF'>*</font> start <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>LONG<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>bits<font color='#5555FF'>+</font>row_width<font color='#5555FF'>*</font>i<font face='Lucida Console'>)</font>;
            LONG<font color='#5555FF'>*</font> end <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>LONG<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>start<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> size <font color='#5555FF'>-</font> padding;
            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>start <font color='#5555FF'>!</font><font color='#5555FF'>=</font> end<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#5555FF'>*</font>start <font color='#5555FF'>=</font> block1;
                <font color='#5555FF'>+</font><font color='#5555FF'>+</font>start;
                <font color='#5555FF'>*</font>start <font color='#5555FF'>=</font> block2;
                <font color='#5555FF'>+</font><font color='#5555FF'>+</font>start;
                <font color='#5555FF'>*</font>start <font color='#5555FF'>=</font> block3;
                <font color='#5555FF'>+</font><font color='#5555FF'>+</font>start;
            <b>}</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>padding<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#5555FF'>*</font>start <font color='#5555FF'>=</font> block1;
                <font color='#5555FF'>+</font><font color='#5555FF'>+</font>start;
                <font color='#5555FF'>-</font><font color='#5555FF'>-</font>padding;
            <b>}</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>padding<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#5555FF'>*</font>start <font color='#5555FF'>=</font> block2;
            <b>}</b>
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='trigger_user_event'></a>trigger_user_event</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> p,
        <font color='#0000FF'><u>int</u></font> i
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;

        user_event_type e;
        e.w <font color='#5555FF'>=</font> hwnd;
        e.p <font color='#5555FF'>=</font> p;
        e.i <font color='#5555FF'>=</font> i;
        <b>{</b>
            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>e<font face='Lucida Console'>)</font>;
        <b>}</b>
        
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>USER_EVENTS_READY,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to schedule function for execution in event handling thread.</font>"<font face='Lucida Console'>)</font>;
        <b>}</b> 
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    base_window::
    <b><a name='base_window'></a>base_window</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>bool</u></font> resizable,
        <font color='#0000FF'><u>bool</u></font> undecorated 
    <font face='Lucida Console'>)</font> :
        globals<font face='Lucida Console'>(</font>gui_core_kernel_1_globals::global_data<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
        has_been_destroyed<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
        prevx<font face='Lucida Console'>(</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>,
        prevy<font face='Lucida Console'>(</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>,
        prev_state<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
        wm<font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.get_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#5555FF'>!</font><font face='Lucida Console'>(</font>undecorated <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> resizable <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\tbase_window::base_window()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tThere is no such thing as an undecorated window that is resizable by the user.</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
            <font face='Lucida Console'>)</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>resizable<font face='Lucida Console'>)</font>   
            style <font color='#5555FF'>=</font> WS_OVERLAPPEDWINDOW;                
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>undecorated<font face='Lucida Console'>)</font>
            style <font color='#5555FF'>=</font> WS_CHILD;
        <font color='#0000FF'>else</font>
            style <font color='#5555FF'>=</font> WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX;

        hwnd <font color='#5555FF'>=</font> gui_core_kernel_1_globals::<font color='#BB00BB'>make_window</font><font face='Lucida Console'>(</font>style<font face='Lucida Console'>)</font>;                

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>hwnd <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>unable to create base_window</font>"<font face='Lucida Console'>)</font>;

        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;

        mouse_in <font color='#5555FF'>=</font> <font color='#979000'>false</font>;

        HWND temp <font color='#5555FF'>=</font> hwnd;
        base_window<font color='#5555FF'>*</font> ttemp <font color='#5555FF'>=</font> <font color='#0000FF'>this</font>;
        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>temp,ttemp<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    base_window::
    ~<b><a name='base_window'></a>base_window</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        <font color='#BB00BB'>close_window</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='close_window'></a>close_window</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// do this just to make sure no one tries to call this window's
</font>            <font color='#009900'>// calbacks.
</font>            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>destroy</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
            gui_core_kernel_1_globals::<font color='#BB00BB'>destroy_window</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
            hwnd <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            has_been_destroyed <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_close_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>  
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='wait_until_closed'></a>wait_until_closed</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_close_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>bool</u></font> base_window::
    <b><a name='is_closed'></a>is_closed</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> has_been_destroyed;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_title'></a>set_title</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::string <font color='#5555FF'>&amp;</font>title
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>title<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_title'></a>set_title</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> ustring <font color='#5555FF'>&amp;</font>title
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_utf32_to_wstring</font><font face='Lucida Console'>(</font>title<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_title'></a>set_title</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::wstring<font color='#5555FF'>&amp;</font> title
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;


        <font color='#009900'>// call the SetWindowText function with our arguments but make sure it is from 
</font>        <font color='#009900'>// the event thread.  We have to do this because the SetWindowText() apparently blocks
</font>        <font color='#009900'>// until something happens in the event thread so we have to 
</font>        <font color='#009900'>// do this to avoid possible deadlocks.
</font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>get_thread_id</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>event_thread_id<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#BB00BB'>SetWindowTextW</font><font face='Lucida Console'>(</font>hwnd,title.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_title <font color='#5555FF'>=</font> title;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>set_window_title_done <font color='#5555FF'>=</font> <font color='#979000'>false</font>;

            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>CALL_SET_WINDOW_TITLE,<font face='Lucida Console'>(</font>WPARAM<font face='Lucida Console'>)</font>hwnd,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to schedule SetWindowText function for execution in event handling thread.</font>"<font face='Lucida Console'>)</font>;
            <b>}</b> 

            <font color='#009900'>// wait for any SetWindowText() calls to finish
</font>            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>set_window_title_done <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='show'></a>show</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>    
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#BB00BB'>show_window</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>style <font color='#5555FF'>!</font><font color='#5555FF'>=</font> WS_CHILD<font face='Lucida Console'>)</font>
            <font color='#BB00BB'>give_window_focus</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='hide'></a>hide</b><font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>    
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#BB00BB'>hide_window</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_size'></a>set_size</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>int</u></font> width_,
        <font color='#0000FF'><u>int</u></font> height_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>get_thread_id</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>event_thread_id<font face='Lucida Console'>)</font>
        <b>{</b>
            RECT info;
            <font color='#BB00BB'>GetWindowRect</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>info<font face='Lucida Console'>)</font>;

            <font color='#0000FF'><u>int</u></font> x <font color='#5555FF'>=</font> info.left;
            <font color='#0000FF'><u>int</u></font> y <font color='#5555FF'>=</font> info.top;
            <font color='#0000FF'><u>int</u></font> width;
            <font color='#0000FF'><u>int</u></font> height;

            RECT rect;
            rect.top <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            rect.left <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            rect.bottom <font color='#5555FF'>=</font> height_;
            rect.right <font color='#5555FF'>=</font> width_;
            <font color='#BB00BB'>AdjustWindowRectEx</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>rect,style,FALSE,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;

            width <font color='#5555FF'>=</font> std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font>rect.right <font color='#5555FF'>-</font> rect.left<font face='Lucida Console'>)</font>;
            height <font color='#5555FF'>=</font> std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font>rect.bottom <font color='#5555FF'>-</font> rect.top<font face='Lucida Console'>)</font>;

            <font color='#BB00BB'>MoveWindow</font><font face='Lucida Console'>(</font>
                hwnd,
                x,
                y,
                width,
                height,
                TRUE<font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            RECT info;
            <font color='#BB00BB'>GetWindowRect</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>info<font face='Lucida Console'>)</font>;

            <font color='#0000FF'><u>int</u></font> x <font color='#5555FF'>=</font> info.left;
            <font color='#0000FF'><u>int</u></font> y <font color='#5555FF'>=</font> info.top;
            <font color='#0000FF'><u>int</u></font> width;
            <font color='#0000FF'><u>int</u></font> height;

            RECT rect;
            rect.top <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            rect.left <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            rect.bottom <font color='#5555FF'>=</font> height_;
            rect.right <font color='#5555FF'>=</font> width_;
            <font color='#BB00BB'>AdjustWindowRectEx</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>rect,style,FALSE,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;

            width <font color='#5555FF'>=</font> std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font>rect.right <font color='#5555FF'>-</font> rect.left<font face='Lucida Console'>)</font>;
            height <font color='#5555FF'>=</font> std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font>rect.bottom <font color='#5555FF'>-</font> rect.top<font face='Lucida Console'>)</font>;

            <font color='#009900'>// call the MoveWindow function with our arguments.  We 
</font>            <font color='#009900'>// have to do this because the MoveWindow() apparently blocks
</font>            <font color='#009900'>// until something happens in the event thread so we have to 
</font>            <font color='#009900'>// do this to avoid possible deadlocks.
</font>            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_hwnd <font color='#5555FF'>=</font> hwnd;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_x <font color='#5555FF'>=</font> x;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_y <font color='#5555FF'>=</font> y;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_width <font color='#5555FF'>=</font> width;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_height <font color='#5555FF'>=</font> height;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_done <font color='#5555FF'>=</font> <font color='#979000'>false</font>;

            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>CALL_MOVE_WINDOW,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to schedule MoveWindow function for execution in event handling thread.</font>"<font face='Lucida Console'>)</font>;
            <b>}</b> 

            <font color='#009900'>// wait for any MoveWindow calls to finish
</font>            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_done <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>

    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_pos'></a>set_pos</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>long</u></font> x_,
        <font color='#0000FF'><u>long</u></font> y_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>get_thread_id</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>event_thread_id<font face='Lucida Console'>)</font>
        <b>{</b>
            RECT info;
            <font color='#BB00BB'>GetWindowRect</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>info<font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>int</u></font> width <font color='#5555FF'>=</font> info.right <font color='#5555FF'>-</font> info.left;
            <font color='#0000FF'><u>int</u></font> height <font color='#5555FF'>=</font> info.bottom <font color='#5555FF'>-</font> info.top;

            <font color='#BB00BB'>MoveWindow</font><font face='Lucida Console'>(</font>
                hwnd,
                x_,
                y_,
                width,
                height,
                TRUE<font face='Lucida Console'>)</font>;

        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            RECT info;
            <font color='#BB00BB'>GetWindowRect</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>info<font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>int</u></font> width <font color='#5555FF'>=</font> info.right <font color='#5555FF'>-</font> info.left;
            <font color='#0000FF'><u>int</u></font> height <font color='#5555FF'>=</font> info.bottom <font color='#5555FF'>-</font> info.top;



            <font color='#009900'>// call the MoveWindow function with our arguments.  We 
</font>            <font color='#009900'>// have to do this because the MoveWindow() apparently blocks
</font>            <font color='#009900'>// until something happens in the event thread so we have to 
</font>            <font color='#009900'>// do this to avoid possible deadlocks.
</font>            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_hwnd <font color='#5555FF'>=</font> hwnd;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_x <font color='#5555FF'>=</font> x_;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_y <font color='#5555FF'>=</font> y_;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_width <font color='#5555FF'>=</font> width;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_height <font color='#5555FF'>=</font> height;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_done <font color='#5555FF'>=</font> <font color='#979000'>false</font>;

            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>PostMessage</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window,WM_USER<font color='#5555FF'>+</font>CALL_MOVE_WINDOW,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>=</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to schedule MoveWindow function for execution in event handling thread.</font>"<font face='Lucida Console'>)</font>;
            <b>}</b> 

            <font color='#009900'>// wait for any MoveWindow calls to finish
</font>            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>move_window_done <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='get_pos'></a>get_pos</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> x_,
        <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> y_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        x_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        y_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        POINT p;
        p.x <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        p.y <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#BB00BB'>ClientToScreen</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>p<font face='Lucida Console'>)</font>;

        x_ <font color='#5555FF'>=</font> p.x;
        y_ <font color='#5555FF'>=</font> p.y;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='get_display_size'></a>get_display_size</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> width,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> height
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        width <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        height <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;


        RECT rc;
        <font color='#BB00BB'>GetWindowRect</font><font face='Lucida Console'>(</font>hwnd, <font color='#5555FF'>&amp;</font>rc<font face='Lucida Console'>)</font>;

        HMONITOR hMonitor;
        MONITORINFO mi;
        <font color='#009900'>//
</font>        <font color='#009900'>// get the nearest monitor to the passed rect.
</font>        <font color='#009900'>//
</font>        hMonitor <font color='#5555FF'>=</font> <font color='#BB00BB'>MonitorFromRect</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>rc, MONITOR_DEFAULTTONEAREST<font face='Lucida Console'>)</font>;

        <font color='#009900'>//
</font>        <font color='#009900'>// get the work area or entire monitor rect.
</font>        <font color='#009900'>//
</font>        mi.cbSize <font color='#5555FF'>=</font> <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>mi<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>GetMonitorInfo</font><font face='Lucida Console'>(</font>hMonitor, <font color='#5555FF'>&amp;</font>mi<font face='Lucida Console'>)</font>;

        rc <font color='#5555FF'>=</font> mi.rcMonitor;

        width <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>rc.right <font color='#5555FF'>-</font> rc.left<font face='Lucida Console'>)</font>;
        height <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>rc.bottom <font color='#5555FF'>-</font> rc.top<font face='Lucida Console'>)</font>;

    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='get_size'></a>get_size</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> width,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> height
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        width <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        height <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;


        RECT r;
        <font color='#BB00BB'>GetClientRect</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>r<font face='Lucida Console'>)</font>;

        width <font color='#5555FF'>=</font> r.right <font color='#5555FF'>-</font> r.left;
        height <font color='#5555FF'>=</font> r.bottom <font color='#5555FF'>-</font> r.top;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='invalidate_rectangle'></a>invalidate_rectangle</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> rectangle<font color='#5555FF'>&amp;</font> rect
    <font face='Lucida Console'>)</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>rect.<font color='#BB00BB'>is_empty</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> <font color='#5555FF'>!</font>has_been_destroyed<font face='Lucida Console'>)</font>
        <b>{</b>
            RECT info;
            info.top <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            info.left <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            info.right <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font>;
            info.bottom <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font>;

            <font color='#BB00BB'>InvalidateRect</font><font face='Lucida Console'>(</font>hwnd,<font color='#5555FF'>&amp;</font>info,FALSE<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_im_pos'></a>set_im_pos</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>long</u></font> x,
        <font color='#0000FF'><u>long</u></font> y
    <font face='Lucida Console'>)</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        HIMC hImc <font color='#5555FF'>=</font> <font color='#BB00BB'>ImmGetContext</font><font face='Lucida Console'>(</font>hwnd<font face='Lucida Console'>)</font>;

        COMPOSITIONFORM cf;
        cf.dwStyle <font color='#5555FF'>=</font> CFS_POINT;
        cf.ptCurrentPos.x <font color='#5555FF'>=</font> x;
        cf.ptCurrentPos.y <font color='#5555FF'>=</font> y;
        <font color='#BB00BB'>ImmSetCompositionWindow</font><font face='Lucida Console'>(</font>hImc, <font color='#5555FF'>&amp;</font>cf<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>ImmReleaseContext</font><font face='Lucida Console'>(</font>hwnd, hImc<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='put_on_clipboard'></a>put_on_clipboard</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>put_on_clipboard</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>str<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='put_on_clipboard'></a>put_on_clipboard</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> dlib::ustring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>put_on_clipboard</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_utf32_to_wstring</font><font face='Lucida Console'>(</font>str<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='put_on_clipboard'></a>put_on_clipboard</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::wstring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;

        shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>OpenClipboard</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#BB00BB'>EmptyClipboard</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

            <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> newlines <font color='#5555FF'>=</font> <font color='#BB00BB'>count</font><font face='Lucida Console'>(</font>str.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,str.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,L'<font color='#FF0000'>\n</font>'<font face='Lucida Console'>)</font>;

            HGLOBAL mem <font color='#5555FF'>=</font> <font color='#BB00BB'>GlobalAlloc</font><font face='Lucida Console'>(</font>GMEM_MOVEABLE,<font face='Lucida Console'>(</font>str.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font>newlines<font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font><font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>wchar_t</u></font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>mem <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>wchar_t</u></font><font color='#5555FF'>*</font> buf <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>wchar_t</u></font><font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GlobalLock</font><font face='Lucida Console'>(</font>mem<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>buf <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#009900'>// copy str into buf while also replacing all the \n with \r\n
</font>                    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font>wstring::size_type i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> str.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>str[i] <font color='#5555FF'>!</font><font color='#5555FF'>=</font> L'<font color='#FF0000'>\n</font>'<font face='Lucida Console'>)</font>
                        <b>{</b>
                            <font color='#5555FF'>*</font>buf <font color='#5555FF'>=</font> str[i];
                            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>buf;
                        <b>}</b>
                        <font color='#0000FF'>else</font>
                        <b>{</b>
                            <font color='#5555FF'>*</font>buf <font color='#5555FF'>=</font> L'<font color='#FF0000'>\r</font>';
                            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>buf;
                            <font color='#5555FF'>*</font>buf <font color='#5555FF'>=</font> L'<font color='#FF0000'>\n</font>';
                            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>buf;
                        <b>}</b>
                    <b>}</b>
                    <font color='#5555FF'>*</font>buf <font color='#5555FF'>=</font> L'<font color='#FF0000'>\0</font>';
                    <font color='#BB00BB'>GlobalUnlock</font><font face='Lucida Console'>(</font>mem<font face='Lucida Console'>)</font>;
                    <font color='#BB00BB'>SetClipboardData</font><font face='Lucida Console'>(</font>CF_UNICODETEXT,mem<font face='Lucida Console'>)</font>;
                <b>}</b>
            <b>}</b>
            <font color='#BB00BB'>CloseClipboard</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='get_from_clipboard'></a>get_from_clipboard</b> <font face='Lucida Console'>(</font>
        std::string<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        std::wstring wstr;
        <font color='#BB00BB'>get_from_clipboard</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
        str <font color='#5555FF'>=</font> <font color='#BB00BB'>convert_wstring_to_mbstring</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='get_from_clipboard'></a>get_from_clipboard</b> <font face='Lucida Console'>(</font>
        dlib::ustring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        std::wstring wstr;
        <font color='#BB00BB'>get_from_clipboard</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
        str <font color='#5555FF'>=</font> <font color='#BB00BB'>convert_wstring_to_utf32</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='get_from_clipboard'></a>get_from_clipboard</b> <font face='Lucida Console'>(</font>
        std::wstring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_1_globals;
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
        shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>OpenClipboard</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>helper_window<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
        <b>{</b>

            HANDLE data <font color='#5555FF'>=</font> <font color='#BB00BB'>GetClipboardData</font><font face='Lucida Console'>(</font>CF_UNICODETEXT<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>data <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>wchar_t</u></font><font color='#5555FF'>*</font> buf <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>wchar_t</u></font><font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#BB00BB'>GlobalLock</font><font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>buf <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    str.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                    <font color='#009900'>// copy the data from buf into str while also removing any '\r' 
</font>                    <font color='#009900'>// characters.
</font>                    <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font>buf <font color='#5555FF'>!</font><font color='#5555FF'>=</font> L'<font color='#FF0000'>\0</font>'<font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font>buf <font color='#5555FF'>!</font><font color='#5555FF'>=</font> L'<font color='#FF0000'>\r</font>'<font face='Lucida Console'>)</font>
                            str <font color='#5555FF'>+</font><font color='#5555FF'>=</font> <font color='#5555FF'>*</font>buf;
                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>buf;
                    <b>}</b>

                    <font color='#BB00BB'>GlobalUnlock</font><font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font>;
                <b>}</b>
                <font color='#0000FF'>else</font>
                <b>{</b>
                    <font color='#BB00BB'>Beep</font><font face='Lucida Console'>(</font><font color='#979000'>500</font>,<font color='#979000'>500</font><font face='Lucida Console'>)</font>;
                <b>}</b>
            <b>}</b>

            <font color='#BB00BB'>CloseClipboard</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>


<font color='#0000FF'>#endif</font> <font color='#009900'>// WIN32
</font>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_GUI_CORE_KERNEL_1_CPp_
</font>

</pre></body></html>